// Copyright (C) 2006 Taylor L. Riché <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

#ifndef MUTATINGQUEUE_HH
#define MUTATINGQUEUE_HH

#include <assert.h>
#include <map>

#ifdef LEE_BUILD
#include "SafeQueue.hh"
#include "Synchronized.hh"
#else
#include <lee/SafeQueue.hh>
#include <lee/Synchronized.hh>
#endif 

namespace lee {

  class PCon;
  class Function;

  class MutatingQueue : public Synchronized
  {
    SafeQueue<PCon *> coreQueue;
    std::map<Function *, uint32_t> workMap;
    uint32_t totalWork;
    uint32_t workThreshold;
    void reduceWork(Function * func, uint32_t work);
    bool addWork(Function * func, uint32_t work);
  public:
    MutatingQueue() : totalWork(0), workThreshold(0) {}
    MutatingQueue(uint32_t t) : totalWork(0), workThreshold(t) {}
    virtual ~MutatingQueue() {}
    PCon * pop();
    PCon * peek();
    bool push(PCon * pcon);
    void forcePush(PCon * pcon);
    inline void setThreshold(uint32_t t) {workThreshold = t;}
  };

}

#endif // MUTATINGQUEUE_HH
